const formatTime = (date: Date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return (
    [year, month, day].map(formatNumber).join('/') +
    ' ' +
    [hour, minute, second].map(formatNumber).join(':')
  )
}

const formatNumber = (n: number) => {
  const s = n.toString()
  return s[1] ? s : '0' + s
}


/**
 * 请求方法,这里就写两种最常用的
 * 其他的请求方式同理(微信官网提供了OPTIONS,HEAD,PUT,DELETE,TRACE,CONNECT)
 */
function request_post(url: string, data: any) {
  let param = packagingParam(url, data, "POST", "application/x-www-form-urlencoded");
  return WXRequest(param);
}
function request_get(url: string, data: any) {
  let param = packagingParam(url, data, "GET", "application/x-www-form-urlencoded");
  return WXRequest(param);
}
function request_json_post(url: string, data: any) {
  let param = packagingParam(url, data, "POST", "application/json");
  return WXRequest(param);
}
function request_json_get(url: string, data: any) {
  let param = packagingParam(url, data, "GET", "application/json");
  return WXRequest(param);
}

/**
 * 封装参数
 */
function packagingParam(url: string, data: Object, method: string, contentType: string) {

  var param = {
    "url": url,
    "data": data,
    "method": method,
    "contentType": contentType,
  };
  return param;
}

/**
 * 发送请求
 * 这里的header只写了一种，一般都需要传输token，用户前后接口的调用的校验
 * 还有的加入了Cookie，加入到header中即可
 */
function WXRequest(param) {
  const apihost = 'https://bi889yu555.vicp.fun';
  //用于请求的计时
  console.time('请求用时：');
  //遮罩提示，这个可以写成公用的调用，这里就简单说明
  // var toast = wx.showLoading({
  //   title: '加载中...',
  //   mask: true
  // })
 
  return new Promise((resolv, reject) => {
    wx.request({
      url: apihost+param.url,
      data: param.data,
      method: param.method,
      header: {
        'Content-Type': param.contentType
      },
      success: function (res) {
        if (res.data == '服务器异常') {
          wx.showModal({
            title: '提示',
            content: '网络错误或服务器繁忙!',
          })
        } else {
          resolv(res.data);
        }
      },
      fail: function (err) {
        reject(err)
        wx.showModal({
          title: '提示',
          content: '网络错误或服务器繁忙!',
        })
      },
      complete: function (com) {
        
        console.log('请求用时：' + param.url);
      }
    })
   
  })
  // wx.hideLoading(toast);
}
//最后把方法暴露出去
module.exports = {
  request_post, request_get, request_json_post, request_json_get, formatTime
}